using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Medianamik.SubsonicDALProvider.DAL
{
	/// <summary>
	/// Strongly-typed collection for the DataType class.
	/// </summary>
	[Serializable]
	public partial class DataTypeCollection : ActiveList<DataType,DataTypeCollection> 
	{	   
		public DataTypeCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the medianamik_DataType table.
	/// </summary>
	[Serializable]
	public partial class DataType : ActiveRecord<DataType>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public DataType()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public DataType(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

        
		public DataType(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public DataType(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }

		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("medianamik_DataType", TableType.Table, DataService.GetInstance("MedianamikProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarDataTypeId = new TableSchema.TableColumn(schema);
				colvarDataTypeId.ColumnName = "DataTypeId";
				colvarDataTypeId.DataType = DbType.Guid;
				colvarDataTypeId.MaxLength = 0;
				colvarDataTypeId.AutoIncrement = false;
				colvarDataTypeId.IsNullable = false;
				colvarDataTypeId.IsPrimaryKey = true;
				colvarDataTypeId.IsForeignKey = false;
				colvarDataTypeId.IsReadOnly = false;
				colvarDataTypeId.DefaultSetting = @"";
				colvarDataTypeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDataTypeId);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.String;
				colvarName.MaxLength = 50;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = false;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarMustSpecifyLength = new TableSchema.TableColumn(schema);
				colvarMustSpecifyLength.ColumnName = "MustSpecifyLength";
				colvarMustSpecifyLength.DataType = DbType.Boolean;
				colvarMustSpecifyLength.MaxLength = 0;
				colvarMustSpecifyLength.AutoIncrement = false;
				colvarMustSpecifyLength.IsNullable = false;
				colvarMustSpecifyLength.IsPrimaryKey = false;
				colvarMustSpecifyLength.IsForeignKey = false;
				colvarMustSpecifyLength.IsReadOnly = false;
				colvarMustSpecifyLength.DefaultSetting = @"";
				colvarMustSpecifyLength.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMustSpecifyLength);
				
				TableSchema.TableColumn colvarMaySpecifyPrecisionAndScale = new TableSchema.TableColumn(schema);
				colvarMaySpecifyPrecisionAndScale.ColumnName = "MaySpecifyPrecisionAndScale";
				colvarMaySpecifyPrecisionAndScale.DataType = DbType.Boolean;
				colvarMaySpecifyPrecisionAndScale.MaxLength = 0;
				colvarMaySpecifyPrecisionAndScale.AutoIncrement = false;
				colvarMaySpecifyPrecisionAndScale.IsNullable = false;
				colvarMaySpecifyPrecisionAndScale.IsPrimaryKey = false;
				colvarMaySpecifyPrecisionAndScale.IsForeignKey = false;
				colvarMaySpecifyPrecisionAndScale.IsReadOnly = false;
				colvarMaySpecifyPrecisionAndScale.DefaultSetting = @"";
				colvarMaySpecifyPrecisionAndScale.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMaySpecifyPrecisionAndScale);
				
				TableSchema.TableColumn colvarDefaultPrecision = new TableSchema.TableColumn(schema);
				colvarDefaultPrecision.ColumnName = "DefaultPrecision";
				colvarDefaultPrecision.DataType = DbType.Int32;
				colvarDefaultPrecision.MaxLength = 0;
				colvarDefaultPrecision.AutoIncrement = false;
				colvarDefaultPrecision.IsNullable = true;
				colvarDefaultPrecision.IsPrimaryKey = false;
				colvarDefaultPrecision.IsForeignKey = false;
				colvarDefaultPrecision.IsReadOnly = false;
				colvarDefaultPrecision.DefaultSetting = @"";
				colvarDefaultPrecision.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDefaultPrecision);
				
				TableSchema.TableColumn colvarDefaultScale = new TableSchema.TableColumn(schema);
				colvarDefaultScale.ColumnName = "DefaultScale";
				colvarDefaultScale.DataType = DbType.Int32;
				colvarDefaultScale.MaxLength = 0;
				colvarDefaultScale.AutoIncrement = false;
				colvarDefaultScale.IsNullable = true;
				colvarDefaultScale.IsPrimaryKey = false;
				colvarDefaultScale.IsForeignKey = false;
				colvarDefaultScale.IsReadOnly = false;
				colvarDefaultScale.DefaultSetting = @"";
				colvarDefaultScale.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDefaultScale);
				
				TableSchema.TableColumn colvarDefaultLength = new TableSchema.TableColumn(schema);
				colvarDefaultLength.ColumnName = "DefaultLength";
				colvarDefaultLength.DataType = DbType.Int32;
				colvarDefaultLength.MaxLength = 0;
				colvarDefaultLength.AutoIncrement = false;
				colvarDefaultLength.IsNullable = true;
				colvarDefaultLength.IsPrimaryKey = false;
				colvarDefaultLength.IsForeignKey = false;
				colvarDefaultLength.IsReadOnly = false;
				colvarDefaultLength.DefaultSetting = @"";
				colvarDefaultLength.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDefaultLength);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MedianamikProvider"].AddSchema("medianamik_DataType",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("DataTypeId")]
		public Guid DataTypeId 
		{
			get { return GetColumnValue<Guid>(Columns.DataTypeId); }

			set { SetColumnValue(Columns.DataTypeId, value); }

		}

		  
		[XmlAttribute("Name")]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }

			set { SetColumnValue(Columns.Name, value); }

		}

		  
		[XmlAttribute("MustSpecifyLength")]
		public bool MustSpecifyLength 
		{
			get { return GetColumnValue<bool>(Columns.MustSpecifyLength); }

			set { SetColumnValue(Columns.MustSpecifyLength, value); }

		}

		  
		[XmlAttribute("MaySpecifyPrecisionAndScale")]
		public bool MaySpecifyPrecisionAndScale 
		{
			get { return GetColumnValue<bool>(Columns.MaySpecifyPrecisionAndScale); }

			set { SetColumnValue(Columns.MaySpecifyPrecisionAndScale, value); }

		}

		  
		[XmlAttribute("DefaultPrecision")]
		public int? DefaultPrecision 
		{
			get { return GetColumnValue<int?>(Columns.DefaultPrecision); }

			set { SetColumnValue(Columns.DefaultPrecision, value); }

		}

		  
		[XmlAttribute("DefaultScale")]
		public int? DefaultScale 
		{
			get { return GetColumnValue<int?>(Columns.DefaultScale); }

			set { SetColumnValue(Columns.DefaultScale, value); }

		}

		  
		[XmlAttribute("DefaultLength")]
		public int? DefaultLength 
		{
			get { return GetColumnValue<int?>(Columns.DefaultLength); }

			set { SetColumnValue(Columns.DefaultLength, value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		private Medianamik.SubsonicDALProvider.DAL.PropertyXCollection colPropertyXRecords;
		public Medianamik.SubsonicDALProvider.DAL.PropertyXCollection PropertyXRecords()
		{
			if(colPropertyXRecords == null)
				colPropertyXRecords = new Medianamik.SubsonicDALProvider.DAL.PropertyXCollection().Where(PropertyX.Columns.DataTypeId, DataTypeId).Load();
			return colPropertyXRecords;
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varDataTypeId,string varName,bool varMustSpecifyLength,bool varMaySpecifyPrecisionAndScale,int? varDefaultPrecision,int? varDefaultScale,int? varDefaultLength)
		{
			DataType item = new DataType();
			
			item.DataTypeId = varDataTypeId;
			
			item.Name = varName;
			
			item.MustSpecifyLength = varMustSpecifyLength;
			
			item.MaySpecifyPrecisionAndScale = varMaySpecifyPrecisionAndScale;
			
			item.DefaultPrecision = varDefaultPrecision;
			
			item.DefaultScale = varDefaultScale;
			
			item.DefaultLength = varDefaultLength;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varDataTypeId,string varName,bool varMustSpecifyLength,bool varMaySpecifyPrecisionAndScale,int? varDefaultPrecision,int? varDefaultScale,int? varDefaultLength)
		{
			DataType item = new DataType();
			
				item.DataTypeId = varDataTypeId;
			
				item.Name = varName;
			
				item.MustSpecifyLength = varMustSpecifyLength;
			
				item.MaySpecifyPrecisionAndScale = varMaySpecifyPrecisionAndScale;
			
				item.DefaultPrecision = varDefaultPrecision;
			
				item.DefaultScale = varDefaultScale;
			
				item.DefaultLength = varDefaultLength;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn DataTypeIdColumn
        {
            get { return Schema.Columns[0]; }

        }

        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[1]; }

        }

        
        
        
        public static TableSchema.TableColumn MustSpecifyLengthColumn
        {
            get { return Schema.Columns[2]; }

        }

        
        
        
        public static TableSchema.TableColumn MaySpecifyPrecisionAndScaleColumn
        {
            get { return Schema.Columns[3]; }

        }

        
        
        
        public static TableSchema.TableColumn DefaultPrecisionColumn
        {
            get { return Schema.Columns[4]; }

        }

        
        
        
        public static TableSchema.TableColumn DefaultScaleColumn
        {
            get { return Schema.Columns[5]; }

        }

        
        
        
        public static TableSchema.TableColumn DefaultLengthColumn
        {
            get { return Schema.Columns[6]; }

        }

        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string DataTypeId = @"DataTypeId";
			 public static string Name = @"Name";
			 public static string MustSpecifyLength = @"MustSpecifyLength";
			 public static string MaySpecifyPrecisionAndScale = @"MaySpecifyPrecisionAndScale";
			 public static string DefaultPrecision = @"DefaultPrecision";
			 public static string DefaultScale = @"DefaultScale";
			 public static string DefaultLength = @"DefaultLength";
						
		}

		#endregion
		
		
		
	}

}

